package com.xt.base.model.base;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.xt.base.dao.hibernate.code.IAutoCreateCode;
import com.xt.base.pojo.BasePOJO;

/**
 * 机构基类 <br>
 * <br>
 * 
 * <p>
 * <a href="Organization.java.html"><i>查看源文件</i></a>
 * </p>
 * 
 * @author 杨洪波
 * @version $Revision: $<br>
 *          $Id: $
 * @hibernate.class table="Organization" dynamic-update="true" dynamic-insert="true" batch-size="200" 
 * 
 */
public class Organization extends BasePOJO implements
		IAutoCreateCode{

	private static final long serialVersionUID = 231743300314619321L;
	
	private String type="Organization";
	
	/** 带拼音的名称 */
	private SpelledField name;


	/** 地址 */
	private Address address;

	/** 成立时间 */
	private Date instaurationTime;

	/** 是否撤销 */
	private boolean closed = false;

	/** 撤销时间 */
	private Date closeTime;
	
	private Organization superior;
	
	private Set<Organization> subOrgas = new HashSet<Organization>();

	/** 机构深度 */
	private int depth;

	public Set<IAutoCreateCode> getTargets() {
		Set<IAutoCreateCode> rSet = super.getTargets();
		if (address != null && !address.isEmpty())
			rSet.addAll(address.getTargets());
		else
			this.setAddress(null);
		return rSet;
	}

	/**
	 * @hibernate.component
	 * @return 名称
	 */
	public SpelledField getName() {
		return name;
	}

	public void setName(SpelledField name) {
		this.name = name;
	}

	/**
	 * @hibernate.property column="Closed" not-null="true"
	 * @return 是否撤销
	 */
	public boolean isClosed() {
		return closed;
	}

	public void setClosed(boolean closed) {
		this.closed = closed;
	}

	/**
	 * @hibernate.property column="CloseTime"
	 * @return 撤销时间
	 */
	public Date getCloseTime() {
		return closeTime;
	}

	public void setCloseTime(Date closeTime) {
		this.closeTime = closeTime;
	}

	/**
	 * @hibernate.property column="InstaurationTime"
	 * @return 成立时间
	 */
	public Date getInstaurationTime() {
		return instaurationTime;
	}

	public void setInstaurationTime(Date instaurationTime) {
		this.instaurationTime = instaurationTime;
	}

	/**
	 * @return 自己是否是某机构的下属机构
	 * @param 机构
	 */
	public boolean isSubordinate(Organization organ) {
		if (organ == null)
			return false;
		//System.out.println(organ.getName().getField());
		//System.out.println(name.getField());
		for (Organization o = getSuperior(); o != null; o = o.getSuperior()){
			//System.out.println(o.getName().getField());
			if (organ.equals(o))
				return true;
		}
		
		return false;
	}

	/**
	 * @hibernate.many-to-one column="Address_ID" cascade="save-update"
	 * @return 地址
	 */
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}


	/**
	 * @hibernate.many-to-one colunm="Superior_ID"
	 * @return
	 */
	public Organization getSuperior() {
		return superior;
	}

	public void setSuperior(Organization superior) {
		this.superior = superior;
	}

	/**
	 * @hibernate.set table="Organization" lazy="true"
	 * @hibernate.collection-key column="SubOrga_ID"
	 * @hibernate.collection-one-to-many class="com.xt.base.model.base.Organization"
	 * @return 下级菜单列表。
	 */
	public Set<Organization> getSubOrgas() {
		return this.subOrgas;
	}

	public void setSubOrgas(Set<Organization> subOrgas) {
		this.subOrgas = subOrgas;
	}

	/**
	 * @hibernate.property colmun = "Depth"
	 * @return
	 */
	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	/**
	 * @hibernate.property colmun = "Type" length="40"
	 * @return
	 */
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	
	/*****************
	 * 返回所有子节点没有被删除的
	 * @return
	 */
	public Set<Organization> getChildsWithNotRemove()
	{
		
		Set<Organization> set = new HashSet<Organization>();
		if(this.subOrgas==null||this.subOrgas.isEmpty())
			return set;
		
		
		for(Organization e:this.subOrgas)
		{
			if(e!=null&&!e.isClosed())
				set.add(e);
		}
		
		return set;
	}

}
